﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GIMS.BLL;
using KPIS.GERP.GIMS.IBLL;
using KPIS.GERP.GIMS.MODEL;
using System.Data;
using System.IO;
using KPIS.GERP.GIMS.APP.APP_CODE;

namespace KPIS.GERP.GIMS.APP.APP_UI.Edit_Form
{
    public partial class ICM_Bid : System.Web.UI.Page
    {
        Common.Main cn = new Common.Main();
        CommandControl cmdFac = new CommandControl();
        BidBLL bllBid = new BidBLL();
        BidderBLL bllBidder = new BidderBLL();
        BidderQuotationBLL bllBidderQuo = new BidderQuotationBLL();
        BidCommitteeBLL bllBidCommittee = new BidCommitteeBLL();
        PurchaseOrderCommitteeBLL bllPoCommittee = new PurchaseOrderCommitteeBLL();
        IBLLHrmEmployee bllEmployee = new HrmEmployeeBLL();
        private const int ItemsPerRequest = 10;
        private const int GridWidth = 150;
        IBLLMasterCommitteeFunction bllComFunction = new MasterCommitteeFunctionBLL();
        IBLLMasterCommitteePosition bllComPosition = new MasterCommitteePositionBLL();
        PurchaseRequestItemBLL bllPrItem = new PurchaseRequestItemBLL();
        IBLLMasterContact bllMasContact = new MasterContactBLL();
        double totalPrice = 0;
        IBLLDocument bllFile = new DocumentBLL();
        SysReportBLL bllReport = new SysReportBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadCommitteePosition();
                LoadCommitteeFunction();
                TipConfirm.TargetControls.Add(btnConfirm.ClientID, true);
                rdbFree.Attributes.Add("onclick", "return ChangeReceiveTypeFree('" + panelDocAmount.ClientID + "')");
                rdbSell.Attributes.Add("onclick", "return ChangeReceiveTypeSell('" + panelDocAmount.ClientID + "')");
                GetKeyValue();
            }

            if (rdbFree.Checked)
            {
                panelDocAmount.Attributes.Add("style", "visibility:hidden");
            }
            if (rdbSell.Checked)
            {
                panelDocAmount.Attributes.Add("style", "visibility:visible");
            }

            if (RadTabStrip1.SelectedTab.Value == "Select")
            {
                LoadGridSelect();
            }
        }

        protected void RadManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            string[] TmpArgument = e.Argument.Split(':');

            if (TmpArgument[0] == "SeqCommitteeClick")
            {
                LoadEditCommittee(Convert.ToInt32(TmpArgument[1]));
            }
            if (TmpArgument[0] == "CloseWindow")
            {
                LoadGridBidder();
            }
            if (TmpArgument[0] == "ReportPdfClick")
            {
                RadManager1.ResponseScripts.Add(@"OpenReport('" + TmpArgument[1] + "','" + GetSeq() + "','pdf');");
            }
            if (TmpArgument[0] == "ReportWordClick")
            {
                RadManager1.ResponseScripts.Add(@"OpenReport('" + TmpArgument[1] + "','" + GetSeq() + "','word');");
            }
            if (TmpArgument[0] == "ReportExcelClick")
            {
                RadManager1.ResponseScripts.Add(@"OpenReport('" + TmpArgument[1] + "','" + GetSeq() + "','excel');");
            }
        }

        protected void OnAjaxUpdate(object sender, ToolTipUpdateEventArgs args)
        {
            this.UpdateToolTip(args.Value, args.UpdatePanel);
        }

        private void UpdateToolTip(string elementID, UpdatePanel panel)
        {
            Control ctrl = Page.LoadControl("../UserControl/TIP_Bid_Confirm.ascx");
            ((KPIS.GERP.GIMS.APP.APP_UI.UserControl.TIP_Bid_Confirm)ctrl).RefreshParentDisplay = new CallBackHandle(RefreshDisplay);
            ((KPIS.GERP.GIMS.APP.APP_UI.UserControl.TIP_Bid_Confirm)ctrl).GetBidSeq = new CallBackHandleReturnInt(GetSeq);
            ((KPIS.GERP.GIMS.APP.APP_UI.UserControl.TIP_Bid_Confirm)ctrl).Alert = new CallBackHandleAlert(Alert);
            panel.ContentTemplateContainer.Controls.Add(ctrl);
        }

        protected void RefreshDisplay()
        {
            RadManager1.ResponseScripts.Add(@"CloseActiveToolTip();");
            //GetKeyValue();
        }

        protected int GetSeq()
        {
            int seq = Convert.ToInt32(Request.QueryString["Seq"]);
            return seq;
        }

        protected void Alert()
        {
            RadManager1.Alert(Session["Alert"].ToString());
        }

        protected void RadTabStrip1_TabClick(object sender, RadTabStripEventArgs e)
        {
            if (RadTabStrip1.SelectedTab.Value == "Detail")
            {
                GetKeyValue();
            }
            else if (RadTabStrip1.SelectedTab.Value == "Item")
            {
                LoadGridItem();
            }
            else if (RadTabStrip1.SelectedTab.Value == "Committee")
            {
                LoadGridCommittee();
            }
            else if (RadTabStrip1.SelectedTab.Value == "Bidder")
            {
                LoadGridBidder();
            }
            else if (RadTabStrip1.SelectedTab.Value == "Judgement")
            {
                LoadJudgement();
                LoadGridJudgement();
            }
            else if (RadTabStrip1.SelectedTab.Value == "File")
            {
                LoadGridFile();
            }
            else if (RadTabStrip1.SelectedTab.Value == "Report")
            {
                LoadGrid_Report();
            }
            //else if (RadTabStrip1.SelectedTab.Value == "Progress")
            //{
            //    LoadGridApprove();
            //    LoadGridInform();
            //    LoadGridRequest();
            //    LoadGridPublishing();
            //    LoadGridCommand();
            //    LoadGridCheck();
            //}
        }

        #region Tab ข้อมูลหลัก

        public void GetKeyValue()
        {
            int seq = GetSeq();
            BidInfo info = bllBid.GetBid(seq);

            lblYearType.Text = info.InfoPr.InfoYearType.Name;
            lblBgmYear.Text = info.InfoPr.BgmYear.ToString();
            lblPcRule.Text = info.InfoPr.InfoPurchaseRule.Name;
            lblPrType.Text = info.InfoPr.PurchaseType;
            lblPrRefNo.Text = info.InfoPr.Ref_No;
            lblPrName.Text = info.InfoPr.Name;

            txtDate.Text = ((DateTime)info.DateBid).ToString("dd/MM/yyyy");
            txtRefNo.Text = info.Ref_no;
            txtName.Text = info.Name;
            lblDocAmountDescr.Text = (info.ReceiveType == "F" ? "แจกฟรี" : "ขายชุดละ " + info.DocumentAmount.ToString() + " บาท");
            rdbFree.Checked = (info.ReceiveType == "F");
            rdbSell.Checked = (info.ReceiveType == "S");
            txtDocAmount.Text = info.DocumentAmount.ToString();
            txtDescr.Text = info.Descr;
            txtPlaceReceive.Text = info.PlaceReceive;
            txtPlaceOffer.Text = info.PlaceOffer;
            txtDateAnnounce.Text = (info.DateAnnounce != null ? ((DateTime)info.DateAnnounce).ToString("dd/MM/yyyy") : "");
            txtDateReceiveStart.Text = (info.DateStartReceive != null ? ((DateTime)info.DateStartReceive).ToString("dd/MM/yyyy") : "");
            txtDateReceiveEnd.Text = (info.DateEndReceive != null ? ((DateTime)info.DateEndReceive).ToString("dd/MM/yyyy") : "");
            txtTimeReceiveStart.Text = (info.TimeStartReceive != null ? ((TimeSpan)info.TimeStartReceive).ToString() : "");
            txtTimeReceiveEnd.Text = (info.TimeEndReceive != null ? ((TimeSpan)info.TimeEndReceive).ToString() : "");
            txtDateBuild.Text = (info.DateBuilding != null ? ((DateTime)info.DateBuilding).ToString("dd/MM/yyyy") : "");
            txtTimeBuildStart.Text = (info.TimeStartBuilding != null ? ((TimeSpan)info.TimeStartBuilding).ToString() : "");
            txtTimeBuildEnd.Text = (info.TimeEndBuilding != null ? ((TimeSpan)info.TimeEndBuilding).ToString() : "");
            txtDateExplain.Text = (info.DateExplain != null ? ((DateTime)info.DateExplain).ToString("dd/MM/yyyy") : "");
            txtTimeExplain.Text = (info.TimeExplain != null ? ((TimeSpan)info.TimeExplain).ToString() : "");
            txtDateOfferStart.Text = (info.DateStartOffer != null ? ((DateTime)info.DateStartOffer).ToString("dd/MM/yyyy") : "");
            txtDateOfferEnd.Text = (info.DateEndOffer != null ? ((DateTime)info.DateEndOffer).ToString("dd/MM/yyyy") : "");
            txtTimeOfferStart.Text = (info.TimeStartOffer != null ? ((TimeSpan)info.TimeStartOffer).ToString() : "");
            txtTimeReceiveEnd.Text = (info.TimeEndOffer != null ? ((TimeSpan)info.TimeEndOffer).ToString() : "");
            txtDateDisclose.Text = (info.DateDisclose != null ? ((DateTime)info.DateDisclose).ToString("dd/MM/yyyy") : "");
            txtTimeDisclose.Text = (info.TimeDisclose != null ? ((TimeSpan)info.TimeDisclose).ToString() : "");
            lblStatus.Text = info.InfoRecord.InfoStatus.Name;

            cmdFac.ViewCommand(panelList_Detail, panelGrid_Detail, panelCommand_Detail);
            cmdFac.SetPanelList(panelDocAmount, "View");
            ManageDisplay(info.InfoRecord.InfoStatus.Status, info.ReceiveType);
        }

        protected void Edit_Detail_Click(object sender, EventArgs e)
        {
            cmdFac.EditCommand(panelList_Detail, panelGrid_Detail, panelCommand_Detail);
            cmdFac.SetPanelList(panelDocAmount, "Edit");
            LoadValiDateDetail();
        }

        protected void EditSave_Detail_Click(object sender, EventArgs e)
        {
            BidInfo info = new BidInfo();
            PurchaseRequestInfo infoPr = new PurchaseRequestInfo();
            HrmEmployeeInfo infoEmployee = new HrmEmployeeInfo();
            HrmJobSiteInfo infoJobSite = new HrmJobSiteInfo();
            SysYearTypeInfo infoYearType = new SysYearTypeInfo();
            RecordInfo infoRecord = new RecordInfo();

            info.Seq = GetSeq();
            info.Ref_no = txtRefNo.Text;
            info.Name = txtName.Text;
            info.Descr = txtDescr.Text;
            info.DateBid = Convert.ToDateTime(txtDate.TextWithLiterals);
            info.DateAnnounce = (txtDateAnnounce.Text.Length > 0 ? (Nullable<DateTime>)Convert.ToDateTime(txtDateAnnounce.TextWithLiterals) : null);
            info.DateStartReceive = (txtDateReceiveStart.Text.Length > 0 ? (Nullable<DateTime>)Convert.ToDateTime(txtDateReceiveStart.TextWithLiterals) : null);
            info.DateEndReceive = (txtDateReceiveEnd.Text.Length > 0 ? (Nullable<DateTime>)Convert.ToDateTime(txtDateReceiveEnd.TextWithLiterals) : null);
            info.DateBuilding = (txtDateBuild.Text.Length > 0 ? (Nullable<DateTime>)Convert.ToDateTime(txtDateBuild.TextWithLiterals) : null);
            info.DateExplain = (txtDateExplain.Text.Length > 0 ? (Nullable<DateTime>)Convert.ToDateTime(txtDateExplain.TextWithLiterals) : null);
            info.DateStartOffer = (txtDateOfferStart.Text.Length > 0 ? (Nullable<DateTime>)Convert.ToDateTime(txtDateOfferStart.TextWithLiterals) : null);
            info.DateEndOffer = (txtDateOfferEnd.Text.Length > 0 ? (Nullable<DateTime>)Convert.ToDateTime(txtDateOfferEnd.TextWithLiterals) : null);
            info.DateDisclose = (txtDateDisclose.Text.Length > 0 ? (Nullable<DateTime>)Convert.ToDateTime(txtDateDisclose.TextWithLiterals) : null);
            info.TimeStartReceive = (txtTimeReceiveStart.Text.Length > 0 ? (Nullable<TimeSpan>)TimeSpan.Parse(txtTimeReceiveStart.TextWithLiterals) : null);
            info.TimeEndReceive = (txtTimeReceiveEnd.Text.Length > 0 ? (Nullable<TimeSpan>)TimeSpan.Parse(txtTimeReceiveEnd.TextWithLiterals) : null);
            info.TimeStartBuilding = (txtTimeBuildStart.Text.Length > 0 ? (Nullable<TimeSpan>)TimeSpan.Parse(txtTimeBuildStart.TextWithLiterals) : null);
            info.TimeEndBuilding = (txtTimeBuildEnd.Text.Length > 0 ? (Nullable<TimeSpan>)TimeSpan.Parse(txtTimeBuildEnd.TextWithLiterals) : null);
            info.TimeExplain = (txtTimeExplain.Text.Length > 0 ? (Nullable<TimeSpan>)TimeSpan.Parse(txtTimeExplain.TextWithLiterals) : null);
            info.TimeStartOffer = (txtTimeOfferStart.Text.Length > 0 ? (Nullable<TimeSpan>)TimeSpan.Parse(txtTimeOfferStart.TextWithLiterals) : null);
            info.TimeEndOffer = (txtTimeOfferEnd.Text.Length > 0 ? (Nullable<TimeSpan>)TimeSpan.Parse(txtTimeOfferEnd.TextWithLiterals) : null);
            info.TimeDisclose = (txtTimeDisclose.Text.Length > 0 ? (Nullable<TimeSpan>)TimeSpan.Parse(txtTimeDisclose.TextWithLiterals) : null);
            info.PlaceReceive = txtPlaceReceive.Text;
            info.PlaceOffer = txtPlaceOffer.Text;
            info.ReceiveType = (rdbFree.Checked ? "F" : "S");
            info.DocumentAmount = (info.ReceiveType == "S" ? (Nullable<int>)Convert.ToInt32(txtDocAmount.Value.ToString()) : null);
            infoRecord.Updated_by = Int32.Parse(Session["EmpSeq"].ToString());
            infoRecord.Updated_when = DateTime.Now;

            info.InfoRecord = infoRecord;

            try
            {
                bllBid.Update(info);
            }
            catch
            {
                RadManager1.Alert(SystemInfo._InsertError);
                return;
            }

            GetKeyValue();
        }

        protected void Cancel_Detail_Click(object sender, EventArgs e)
        {
            cmdFac.CancelCommand(panelList_Detail, panelGrid_Detail, panelCommand_Detail);
            cmdFac.SetPanelList(panelDocAmount, "Cancel");
        }

        private void LoadValiDateDetail()
        {
            RequiredFieldValidator valDate = new RequiredFieldValidator();
            valDate.ID = "ValtxtDate";
            valDate.ControlToValidate = "txtDate";
            valDate.SetFocusOnError = true;
            valDate.Display = ValidatorDisplay.Dynamic;
            valDate.ErrorMessage = "* กรุณาระบุวันที่รายการ *";
            panelValtxtDate.Controls.Add(valDate);
            CustomValidator valCustomDate = new CustomValidator();
            valCustomDate.ID = "ValCustomDate";
            valCustomDate.ControlToValidate = "txtDate";
            valCustomDate.SetFocusOnError = true;
            valCustomDate.Display = ValidatorDisplay.Dynamic;
            valCustomDate.ErrorMessage = "* กรุณาระบุวันที่ให้ถูกต้อง *";
            valCustomDate.ClientValidationFunction = "validateDate";
            panelValtxtDate.Controls.Add(valCustomDate);
            RequiredFieldValidator valRefNo = new RequiredFieldValidator();
            valRefNo.ID = "ValtxtRefNo";
            valRefNo.ControlToValidate = "txtRefNo";
            valRefNo.SetFocusOnError = true;
            valRefNo.Display = ValidatorDisplay.Dynamic;
            valRefNo.ErrorMessage = "* กรุณาระบุเลขที่ประกาศ *";
            panelValtxtRefNo.Controls.Add(valRefNo);
            RequiredFieldValidator valName = new RequiredFieldValidator();
            valName.ID = "ValtxtName";
            valName.ControlToValidate = "txtName";
            valName.SetFocusOnError = true;
            valName.Display = ValidatorDisplay.Dynamic;
            valName.ErrorMessage = "* กรุณาระบุเรื่องประกาศ *";
            panelValtxtName.Controls.Add(valName);
            CustomValidator valAnnounce = new CustomValidator();
            valAnnounce.ID = "ValtxtDateAnnounce";
            valAnnounce.ControlToValidate = "txtDateAnnounce";
            valAnnounce.SetFocusOnError = true;
            valAnnounce.Display = ValidatorDisplay.Dynamic;
            valAnnounce.ErrorMessage = "* กรุณาระบุวันที่ลงนามประกาศให้ถูกต้อง *";
            valAnnounce.ClientValidationFunction = "validateDate";
            panelValAnnounce.Controls.Add(valAnnounce);
            CustomValidator valReceiveStart = new CustomValidator();
            valReceiveStart.ID = "ValtxtDateReceiveStart";
            valReceiveStart.ControlToValidate = "txtDateReceiveStart";
            valReceiveStart.SetFocusOnError = true;
            valReceiveStart.Display = ValidatorDisplay.Dynamic;
            valReceiveStart.ErrorMessage = "* กรุณาระบุวันที่เริ่มจำหน่ายเอกสารให้ถูกต้อง *";
            valReceiveStart.ClientValidationFunction = "validateDate";
            panelValReceive.Controls.Add(valReceiveStart);
            CustomValidator valReceiveEnd = new CustomValidator();
            valReceiveEnd.ID = "ValtxtDateReceiveEnd";
            valReceiveEnd.ControlToValidate = "txtDateReceiveEnd";
            valReceiveEnd.SetFocusOnError = true;
            valReceiveEnd.Display = ValidatorDisplay.Dynamic;
            valReceiveEnd.ErrorMessage = "* กรุณาระบุวันที่สิ้นสุดจำหน่ายเอกสารให้ถูกต้อง *";
            valReceiveEnd.ClientValidationFunction = "validateDate";
            panelValReceive.Controls.Add(valReceiveEnd);
            CustomValidator valBuild = new CustomValidator();
            valBuild.ID = "ValtxtDateBuild";
            valBuild.ControlToValidate = "txtDateBuild";
            valBuild.SetFocusOnError = true;
            valBuild.Display = ValidatorDisplay.Dynamic;
            valBuild.ErrorMessage = "* กรุณาระบุวันที่ดูสถานที่ให้ถูกต้อง *";
            valBuild.ClientValidationFunction = "validateDate";
            panelValBuild.Controls.Add(valBuild);
            CustomValidator valExplain = new CustomValidator();
            valExplain.ID = "ValtxtDateExplain";
            valExplain.ControlToValidate = "txtDateExplain";
            valExplain.SetFocusOnError = true;
            valExplain.Display = ValidatorDisplay.Dynamic;
            valExplain.ErrorMessage = "* กรุณาระบุวันที่ชี้แจงรายละเอียดให้ถูกต้อง *";
            valExplain.ClientValidationFunction = "validateDate";
            panelValExplain.Controls.Add(valExplain);
            CustomValidator valOfferStart = new CustomValidator();
            valOfferStart.ID = "ValtxtDateOfferStart";
            valOfferStart.ControlToValidate = "txtDateOfferStart";
            valOfferStart.SetFocusOnError = true;
            valOfferStart.Display = ValidatorDisplay.Dynamic;
            valOfferStart.ErrorMessage = "* กรุณาระบุวันที่เริ่มยื่นซองให้ถูกต้อง *";
            valOfferStart.ClientValidationFunction = "validateDate";
            panelValOffer.Controls.Add(valOfferStart);
            CustomValidator valOfferEnd = new CustomValidator();
            valOfferEnd.ID = "ValtxtDateOfferEnd";
            valOfferEnd.ControlToValidate = "txtDateOfferEnd";
            valOfferEnd.SetFocusOnError = true;
            valOfferEnd.Display = ValidatorDisplay.Dynamic;
            valOfferEnd.ErrorMessage = "* กรุณาระบุวันที่สิ้นสุดยื่นซองให้ถูกต้อง *";
            valOfferEnd.ClientValidationFunction = "validateDate";
            panelValOffer.Controls.Add(valOfferEnd);
            CustomValidator valDisclose = new CustomValidator();
            valDisclose.ID = "ValtxtDateDisclose";
            valDisclose.ControlToValidate = "txtDateDisclose";
            valDisclose.SetFocusOnError = true;
            valDisclose.Display = ValidatorDisplay.Dynamic;
            valDisclose.ErrorMessage = "* กรุณาระบุวันที่เปิดซอง/เสนอาคาให้ถูกต้อง *";
            valDisclose.ClientValidationFunction = "validateDate";
            panelValDisclose.Controls.Add(valDisclose);
        }

        protected void ManageDisplay(string Status, string ReceiveType)
        {
            switch (Status)
            {
                case "A":
                case "P":
                    // tab ข้อมูลหลัก
                    panelCommand_Detail.Visible = false;
                    // tab รายชื่อคณะกรรมการ
                    panelCommand_Committee.Visible = false;
                    // tab รายละเอียดผู้มาติดต่อ
                    panelCommand_Bidder.Visible = false;
                    // tab เอกสารแนบ
                    panelCommand_File.Visible = false;
                    btnConfirm.Visible = false;
                    btnDeny.Visible = true;
                    break;
                case "C":
                    // tab ข้อมูลหลัก
                    panelCommand_Detail.Visible = false;
                    // tab รายชื่อคณะกรรมการ
                    panelCommand_Committee.Visible = false;
                    // tab รายละเอียดผู้มาติดต่อ
                    panelCommand_Bidder.Visible = false;
                    // tab เอกสารแนบ
                    panelCommand_File.Visible = false;
                    btnConfirm.Visible = false;
                    btnDeny.Visible = false;
                    break;
                case "N":
                case "R":
                case "W":
                    // tab ข้อมูลหลัก
                    panelCommand_Detail.Visible = true;
                    // tab รายชื่อคณะกรรมการ
                    panelCommand_Committee.Visible = true;
                    // tab รายละเอียดผู้มาติดต่อ
                    panelCommand_Bidder.Visible = (ReceiveType == "F");
                    // tab เอกสารแนบ
                    panelCommand_File.Visible = true;
                    btnConfirm.Visible = true;
                    btnDeny.Visible = false;
                    break;
            }

            Grid_Bidder.Columns[2].Visible = (ReceiveType != "F");
        }

        #endregion

        //-----------------------------------------------------------------------------------------------

        #region tab รายการพัสดุ

        protected void LoadGridItem()
        {
            int seq = GetSeq();
            IList<PurchaseRequestItemInfo> infoPrItem = bllPrItem.GetPurchaseRequestItemFromBid(seq);
            Grid_Item.DataSource = infoPrItem;
            Grid_Item.MasterTableView.GroupByExpressions.Clear();
            GridGroupByExpression geExpense = new GridGroupByExpression(Grid_Item.Columns[0]);
            Grid_Item.MasterTableView.GroupByExpressions.Add(geExpense);
            Grid_Item.DataBind();
        }

        protected void Grid_Item_ItemCommand(object source, GridCommandEventArgs e)
        {
            LoadGridItem();
        }

        protected void Grid_Item_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                double qty = 0;
                double price = 0;
                GridDataItem dataBoundItem = e.Item as GridDataItem;

                if (((Label)(dataBoundItem["Qty"].FindControl("lblQty"))).Text.Length > 0)
                {
                    qty = double.Parse(((Label)(dataBoundItem["Qty"].FindControl("lblQty"))).Text);
                }
                if (((Label)(dataBoundItem["Price"].FindControl("lblPrice"))).Text.Length > 0)
                {
                    price = double.Parse(((Label)(dataBoundItem["Price"].FindControl("lblPrice"))).Text);
                }

                totalPrice += (qty * price);

                //if (!Object.Equals(target, null))
                //{
                //    if (!Object.Equals(this.TipItemDetail, null))
                //    {
                //        this.TipItemDetail.TargetControls.Add(target.ClientID, "Item:" + dataBoundItem["InfoItem.Seq"].Text, true);
                //    }
                //}
            }
            else if (e.Item is GridFooterItem)
            {
                GridFooterItem footer = (GridFooterItem)e.Item;
                footer["Descr"].Controls.Add(new LiteralControl("<div align='right' style='color: Black; font-weight: bold;'>วงเงินจัดหาทั้งหมด :</div> "));
                ((Label)footer["Price"].FindControl("lblTotalPrice")).Text = totalPrice.ToString("###,###,##0.00");
            }
        }

        #endregion

        //-----------------------------------------------------------------------------------------------

        #region tab คณะกรรมการ

        protected void LoadGridCommittee()
        {
            int seq = GetSeq();
            Grid_Committee.DataSource = bllBidCommittee.GetBidCommitteeWithParent(seq);
            GridGroupByExpression geFunction = new GridGroupByExpression(Grid_Committee.Columns[2]);
            GridGroupByExpression geRefNo = new GridGroupByExpression(Grid_Committee.Columns[3]);
            Grid_Committee.MasterTableView.GroupByExpressions.Add(geFunction);
            Grid_Committee.MasterTableView.GroupByExpressions.Add(geRefNo);
            Grid_Committee.DataBind();
            cmdFac.GridCommand(panelList_Committee, panelGrid_Committee, panelCommand_Committee);
        }

        protected void LoadCommitteePosition()
        {
            IList<MasterCommitteePositionInfo> infoList = bllComPosition.GetMasterCommitteePosition();
            cbCommitteePosition.DataSource = infoList;
            cbCommitteePosition.DataBind();
        }

        protected void LoadCommitteeFunction()
        {
            IList<MasterCommitteeFunctionInfo> infoList = bllComFunction.GetMasterCommitteeFunction();
            cbCommitteeFunction.DataSource = infoList;
            cbCommitteeFunction.DataBind();
        }

        protected void Grid_Committee_ItemCommand(object source, GridCommandEventArgs e)
        {
            LoadGridCommittee();
        }

        protected void rblCommitteeType_SelectedIndexChanged(object sender, EventArgs e)
        {
            panelInternal.Visible = (rblCommitteeType.SelectedValue == "1");
            panelExternal.Visible = (rblCommitteeType.SelectedValue == "2");
            LoadValidateCommittee();
        }

        protected void cbCommitteeInternalName_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
        {
            IList<HrmEmployeeInfo> infoList = bllEmployee.GetHrmEmployee(e.Text);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, infoList.Count);
            e.EndOfItems = (endOffset == infoList.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cbCommitteeInternalName.Items.Add(new RadComboBoxItem(infoList[i].Name, infoList[i].Seq.ToString()));
                TableRow tr = new TableRow();
                TableCell tdJobSite = new TableCell();
                Label lblJobSite = new Label();
                lblJobSite.Text = infoList[i].InfoJobSite.Name;
                tdJobSite.Width = Unit.Pixel(200);
                tdJobSite.Controls.Add(lblJobSite);
                tr.Cells.Add(tdJobSite);
                TableCell tdName = new TableCell();
                Label lblName = new Label();
                lblName.Text = infoList[i].Name;
                tdName.Width = Unit.Pixel(150);
                tdName.Controls.Add(lblName);
                tr.Cells.Add(tdName);
                TableCell tdPosition = new TableCell();
                Label lblPosition = new Label();
                lblPosition.Text = infoList[i].InfoPosition.Name;
                tdPosition.Width = Unit.Pixel(150);
                tdPosition.Controls.Add(lblPosition);
                tr.Cells.Add(tdPosition);
                TableCell tdClass = new TableCell();
                Label lblClass = new Label();
                lblClass.Text = infoList[i].InfoClass.Name;
                tdClass.Width = Unit.Pixel(190);
                tdClass.Controls.Add(lblClass);
                tr.Cells.Add(tdClass);
                ((Table)cbCommitteeInternalName.Items[j].FindControl("tableCommmitteeInternalName")).Controls.Add(tr);
                j++;
            }

            e.Message = GetStatusMessage(endOffset, infoList.Count);
        }

        private static string GetStatusMessage(int offset, int total)
        {
            if (total <= 0)
                return "ไม่มีข้อมูล";

            return String.Format("รายการที่ <b>1</b>-<b>{0}</b> จากทั้งหมด <b>{1}</b> รายการ", offset, total);
        }

        protected void OnAjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            RadAjaxPanel1.Controls.Add(Page.LoadControl("../UserControl/TIP_Bid_Committee_History.ascx"));
        }

        protected void ShowGrid_Committee_Click(object sender, EventArgs e)
        {
            LoadGridCommittee();
        }

        protected void Edit_Committee_Click(object sender, EventArgs e)
        {
            cmdFac.EditCommand(panelList_Committee, panelGrid_Committee, panelCommand_Committee);
            cmdFac.SetPanelList(panelInternal, "Edit");
            cmdFac.SetPanelList(panelExternal, "Edit");
            LoadValidateCommittee();
        }

        protected void AddNew_Committee_Click(object sender, EventArgs e)
        {
            cmdFac.AddNewCommand(panelList_Committee, panelGrid_Committee, panelCommand_Committee);
            LoadValidateCommittee();
        }

        protected void btnDelete_Committee_Click(object sender, EventArgs e)
        {
            IList<BidCommitteeInfo> infoList = new List<BidCommitteeInfo>();
            if (panelGrid_Committee.Visible)
            {
                foreach (GridDataItem data in Grid_Committee.MasterTableView.GetSelectedItems())
                {
                    BidCommitteeInfo info = new BidCommitteeInfo();
                    RecordInfo infoRecord = new RecordInfo();
                    info.Seq = Convert.ToInt32(data.OwnerTableView.DataKeyValues[data.ItemIndex]["Seq"]);
                    info.DeleteReason = txtDeleteReason.Text;
                    infoRecord.Updated_by = Int32.Parse(Session["EmpSeq"].ToString());
                    infoRecord.Updated_when = DateTime.Now;
                    info.InfoRecord = infoRecord;
                    infoList.Add(info);
                }
            }
            else
            {
                BidCommitteeInfo info = new BidCommitteeInfo();
                RecordInfo infoRecord = new RecordInfo();
                info.Seq = Convert.ToInt32(hdCommitteeSeq.Value);
                info.DeleteReason = txtDeleteReason.Text;
                infoRecord.Updated_by = Int32.Parse(Session["EmpSeq"].ToString());
                infoRecord.Updated_when = DateTime.Now;
                info.InfoRecord = infoRecord;
                infoList.Add(info);
            }

            if (infoList.Count == 0)
            {
                RadManager1.Alert(SystemInfo._DeleteNull);
                return;
            }

            try
            {
                bllBidCommittee.DeleteAll(infoList);
            }
            catch
            {
                RadManager1.Alert(SystemInfo._DeleteError);
                return;
            }

            LoadGridCommittee();
        }

        protected void Delete_Committee_Click(object sender, EventArgs e)
        {
            IList<BidCommitteeInfo> infoList = new List<BidCommitteeInfo>();
            if (panelGrid_Committee.Visible)
            {
                foreach (GridDataItem data in Grid_Committee.MasterTableView.GetSelectedItems())
                {
                    BidCommitteeInfo info = new BidCommitteeInfo();
                    info.Seq = Convert.ToInt32(data.OwnerTableView.DataKeyValues[data.ItemIndex]["Seq"]);
                    infoList.Add(info);
                }
            }
            else
            {
                BidCommitteeInfo info = new BidCommitteeInfo();
                info.Seq = Convert.ToInt32(hdCommitteeSeq.Value);
                infoList.Add(info);
            }

            if (infoList.Count == 0)
            {
                RadManager1.Alert(SystemInfo._DeleteNull);
                return;
            }

            try
            {
                bllBidCommittee.DeleteFromDB(infoList);
            }
            catch
            {
                RadManager1.Alert(SystemInfo._DeleteError);
                return;
            }

            LoadGridCommittee();
        }

        protected void AddNewSave_Committee_Click(object sender, EventArgs e)
        {
            BidCommitteeInfo info = new BidCommitteeInfo();
            HrmEmployeeInfo infoEmployee = new HrmEmployeeInfo();
            MasterCommitteePositionInfo infoPosition = new MasterCommitteePositionInfo();
            MasterCommitteeFunctionInfo infoFunction = new MasterCommitteeFunctionInfo();
            HrmEmployeeInfo infoEmployeeDetail = new HrmEmployeeInfo();
            RecordInfo infoRecord = new RecordInfo();
            SysCommitteeTypeInfo infoType = new SysCommitteeTypeInfo();
            BidInfo infoBid = new BidInfo();

            infoBid.Seq = GetSeq();
            info.Ref_no = Convert.ToInt32(txtCommitteeRefno.Value);
            infoType.Seq = Convert.ToInt32(rblCommitteeType.SelectedValue);
            infoType.Name = (rblCommitteeType.SelectedItem.Text);
            infoPosition.Seq = Convert.ToInt32(cbCommitteePosition.SelectedValue);
            infoPosition.Name = cbCommitteePosition.SelectedItem.Text;
            infoFunction.Seq = Convert.ToInt32(cbCommitteeFunction.SelectedValue);
            infoFunction.Name = cbCommitteeFunction.SelectedItem.Text;
            if (infoType.Seq == 1) //บุคลากรภายใน
            {
                infoEmployee.Seq = Convert.ToInt32(cbCommitteeInternalName.SelectedValue);
            }
            else
            {
                info.Ex_name = txtCommitteeExternalName.Text;
                info.Ex_descr = txtCommitteeExternalDescr.Text;
            }
            infoRecord.Created_by = cn.UserID;
            infoRecord.Created_when = DateTime.Now;
            info.InfoBid = infoBid;
            info.InfoType = infoType;
            info.InfoExployee = infoEmployee;
            info.InfoPosition = infoPosition;
            info.InfoFunction = infoFunction;
            info.InfoRecord = infoRecord;

            try
            {
                bllBidCommittee.Insert(info);
                ClearCommittee();
                LoadGridCommittee();
            }
            catch
            {
                RadManager1.Alert(SystemInfo._InsertError);
                return;
            }
        }

        protected void EditSave_Committee_Click(object sender, EventArgs e)
        {
            BidCommitteeInfo info = new BidCommitteeInfo();
            HrmEmployeeInfo infoEmployee = new HrmEmployeeInfo();
            MasterCommitteePositionInfo infoPosition = new MasterCommitteePositionInfo();
            MasterCommitteeFunctionInfo infoFunction = new MasterCommitteeFunctionInfo();
            HrmEmployeeInfo infoEmployeeDetail = new HrmEmployeeInfo();
            RecordInfo infoRecord = new RecordInfo();
            SysCommitteeTypeInfo infoType = new SysCommitteeTypeInfo();
            info.Seq = Convert.ToInt32(hdCommitteeSeq.Value);
            info.Ref_no = Convert.ToInt32(txtCommitteeRefno.Value);
            infoType.Seq = Convert.ToInt32(rblCommitteeType.SelectedValue);
            infoType.Name = (rblCommitteeType.SelectedItem.Text);
            infoPosition.Seq = Convert.ToInt32(cbCommitteePosition.SelectedValue);
            infoPosition.Name = cbCommitteePosition.SelectedItem.Text;
            infoFunction.Seq = Convert.ToInt32(cbCommitteeFunction.SelectedValue);
            infoFunction.Name = cbCommitteeFunction.SelectedItem.Text;
            if (infoType.Seq == 1) //บุคลากรภายใน
            {
                infoEmployee.Seq = Convert.ToInt32(cbCommitteeInternalName.SelectedValue);
            }
            else
            {
                info.Ex_name = txtCommitteeExternalName.Text;
                info.Ex_descr = txtCommitteeExternalDescr.Text;
            }
            infoRecord.Updated_by = Int32.Parse(Session["EmpSeq"].ToString());
            infoRecord.Updated_when = DateTime.Now;
            info.InfoType = infoType;
            info.InfoExployee = infoEmployee;
            info.InfoPosition = infoPosition;
            info.InfoFunction = infoFunction;
            info.InfoRecord = infoRecord;
            try
            {
                bllBidCommittee.Update(info);
                LoadGridCommittee();
            }
            catch
            {
                RadManager1.Alert(SystemInfo._InsertError);
                return;
            }
        }

        protected void Cancel_Committee_Click(object sender, EventArgs e)
        {
            LoadEditCommittee(Convert.ToInt32(hdCommitteeSeq.Value));
        }

        protected void ClearCommittee()
        {
            txtCommitteeRefno.Text = "";
            rblCommitteeType.Items[0].Selected = true;
            rblCommitteeType.Items[1].Selected = false;
            panelInternal.Visible = true;
            panelExternal.Visible = false;
            cbCommitteeInternalName.SelectedIndex = -1;
            cbCommitteeInternalName.Text = "";
            txtCommitteeExternalName.Text = "";
            txtCommitteeExternalDescr.Text = "";
            cbCommitteePosition.SelectedIndex = -1;
            cbCommitteePosition.Text = "";
            cbCommitteeFunction.SelectedIndex = -1;
            cbCommitteeFunction.Text = "";
            
        }

        protected void LoadEditCommittee(int ComSeq)
        {
            BidCommitteeInfo info = bllBidCommittee.GetBidCommittee(ComSeq);
            txtCommitteeRefno.Text = info.Ref_no.ToString();
            lblCommitteeRefNo.Text = info.Ref_no.ToString();
            rblCommitteeType.SelectedValue = info.InfoType.Seq.ToString();
            if (info.InfoType.Seq == 1)
            {
                panelInternal.Visible = true;
                panelExternal.Visible = false;
                cbCommitteeInternalName.SelectedValue = info.InfoExployee.Seq.ToString();
                cbCommitteeInternalName.Text = info.InfoExployee.Name;
                lblCommitteeInternalName.Text = info.InfoExployee.Name;
            }
            else
            {
                panelInternal.Visible = false;
                panelExternal.Visible = true;
                txtCommitteeExternalName.Text = info.Ex_name;
                txtCommitteeExternalDescr.Text = info.Ex_descr;
            }

            cbCommitteePosition.SelectedValue = info.InfoPosition.Seq.ToString();
            cbCommitteeFunction.SelectedValue = info.InfoFunction.Seq.ToString();
            cmdFac.ViewCommand(panelList_Committee, panelGrid_Committee, panelCommand_Committee);
            cmdFac.SetViewMode(panelInternal);
            cmdFac.SetPanelList(panelInternal, "View");
            cmdFac.SetViewMode(panelExternal);
            cmdFac.SetPanelList(panelExternal, "View");
        }

        protected void Grid_Committee_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                GridDataItem dataBoundItem = e.Item as GridDataItem;
                Image image = (Image)dataBoundItem["Image"].FindControl("Image1");
                if (dataBoundItem["InfoRecord.InfoStatus.Status"].Text == "W")
                {
                    image.ImageUrl = "../Image/Grid/Update.gif";
                }
                else
                {
                    image.ImageUrl = "../Image/Grid/Cancel.gif";
                }
            }
        }

        protected void LoadValidateCommittee()
        {
            RequiredFieldValidator valCommitteeRefno = new RequiredFieldValidator();
            valCommitteeRefno.ID = "ValtxtCommitteeRefno";
            valCommitteeRefno.ControlToValidate = "txtCommitteeRefno";
            valCommitteeRefno.SetFocusOnError = true;
            valCommitteeRefno.Display = ValidatorDisplay.Dynamic;
            valCommitteeRefno.ErrorMessage = "* กรุณาระบุชุดที่ *";
            panelValtxtCommitteeRefno.Controls.Add(valCommitteeRefno);
            RequiredFieldValidator valCommitteeInternalName = new RequiredFieldValidator();
            valCommitteeInternalName.ID = "ValcbCommitteeInternalName";
            valCommitteeInternalName.ControlToValidate = "cbCommitteeInternalName";
            valCommitteeInternalName.SetFocusOnError = true;
            valCommitteeInternalName.Display = ValidatorDisplay.Dynamic;
            valCommitteeInternalName.ErrorMessage = "* กรุณาระบุชื่อกรรมการ *";
            panelValcbCommitteeInternalName.Controls.Add(valCommitteeInternalName);
            RequiredFieldValidator valCommitteeExternalName = new RequiredFieldValidator();
            valCommitteeExternalName.ID = "ValtxtCommitteeExternalName";
            valCommitteeExternalName.ControlToValidate = "txtCommitteeExternalName";
            valCommitteeExternalName.SetFocusOnError = true;
            valCommitteeExternalName.Display = ValidatorDisplay.Dynamic;
            valCommitteeExternalName.ErrorMessage = "* กรุณาระบุชื่อกรรมการ *";
            panelValtxtCommitteeExternalName.Controls.Add(valCommitteeExternalName);
            RequiredFieldValidator valCommitteePosition = new RequiredFieldValidator();
            valCommitteePosition.ID = "ValcbCommitteePosition";
            valCommitteePosition.ControlToValidate = "cbCommitteePosition";
            valCommitteePosition.SetFocusOnError = true;
            valCommitteePosition.Display = ValidatorDisplay.Dynamic;
            valCommitteePosition.ErrorMessage = "* กรุณาระบุตำแหน่งกรรมการ *";
            panelValcbCommitteePosition.Controls.Add(valCommitteePosition);
            RequiredFieldValidator valCommitteeFunction = new RequiredFieldValidator();
            valCommitteeFunction.ID = "ValcbCommitteeFunction";
            valCommitteeFunction.ControlToValidate = "cbCommitteeFunction";
            valCommitteeFunction.SetFocusOnError = true;
            valCommitteeFunction.Display = ValidatorDisplay.Dynamic;
            valCommitteeFunction.ErrorMessage = "* กรุณาระบุหน้าที่กรรมการ *";
            panelValcbCommitteeFunction.Controls.Add(valCommitteeFunction);
        }

        #endregion

        #region tab รายละเอียดผู้มาติดต่อ

        protected void LoadGridBidder()
        {
            int seq = GetSeq();
            IList<BidderInfo> infoBidder = bllBidder.GetBidderFromBid(seq);
            Grid_Bidder.DataSource = infoBidder;
            GridGroupByExpression geName = new GridGroupByExpression(Grid_Bidder.Columns[1]);
            Grid_Bidder.MasterTableView.GroupByExpressions.Add(geName);
            Grid_Bidder.DataBind();
        }

        protected void Grid_Bidder_ItemCommand(object source, GridCommandEventArgs e)
        {
            LoadGridBidder();
        }

        protected void Grid_Bidder_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                GridDataItem dataBoundItem = e.Item as GridDataItem;
                Image imageReceive = (Image)dataBoundItem["ReceiveFlag"].FindControl("imgFlagReceive");
                Image imageOffer = (Image)dataBoundItem["OfferFlag"].FindControl("imgFlagOffer");
                Image imageWinner = (Image)dataBoundItem["WinnerFlag"].FindControl("imgFlagWinner");

                imageReceive.ImageUrl = "../Image/Grid/Cancel.gif";
                imageOffer.ImageUrl = "../Image/Grid/Cancel.gif";
                imageWinner.ImageUrl = "../Image/Grid/Cancel.gif";

                if (dataBoundItem["ReceiveStatus"].Text == "Y")
                {
                    imageReceive.ImageUrl = "../Image/Grid/Update.gif";
                }
                if (dataBoundItem["OfferStatus"].Text == "Y")
                {
                    imageOffer.ImageUrl = "../Image/Grid/Update.gif";
                }
                if (dataBoundItem["WinnerStatus"].Text == "Y")
                {
                    imageWinner.ImageUrl = "../Image/Grid/Update.gif";
                }
            }
        }

        protected void cbContactReceive_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
        {
            IList<MasterContactInfo> infoList = bllMasContact.GetMasterContactNoBlackList(e.Text);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, infoList.Count);
            e.EndOfItems = (endOffset == infoList.Count);
            int j = 0; 
            for (int i = itemOffset; i < endOffset; i++)
            {
                cbContactReceive.Items.Add(new RadComboBoxItem(infoList[i].Name_th, infoList[i].Seq.ToString()));
                TableRow tr = new TableRow();
                TableCell tdGroup = new TableCell();
                Label lblGroup = new Label();
                lblGroup.Text = infoList[i].InfoGroup.Name;
                tdGroup.Width = Unit.Pixel(100);
                tdGroup.Controls.Add(lblGroup);
                tr.Cells.Add(tdGroup);

                TableCell tdCode = new TableCell();
                Label lblCode = new Label();
                lblCode.Text = infoList[i].Gfmis_code;
                tdCode.Width = Unit.Pixel(150);
                tdCode.Controls.Add(lblCode);
                tr.Cells.Add(tdCode);

                TableCell tdName = new TableCell();
                Label lblName = new Label();
                lblName.Text = infoList[i].Name_th;
                tdName.Width = Unit.Pixel(150);
                tdName.Controls.Add(lblName);
                tr.Cells.Add(tdName);

                TableCell tdAddress = new TableCell();
                Label lblAddress = new Label();
                lblAddress.Text = infoList[i].Address;
                tdAddress.Width = Unit.Pixel(100);
                tdAddress.Controls.Add(lblAddress);
                tr.Cells.Add(tdAddress);
                ((Table)cbContactReceive.Items[j].FindControl("tableContactReceive")).Controls.Add(tr);
                j++;
            }

            e.Message = GetStatusMessage(endOffset, infoList.Count);
        }

        protected void Delete_Bidder_Click(object sender, EventArgs e)
        {
            IList<BidderInfo> infoList = new List<BidderInfo>();
            foreach (GridDataItem data in Grid_Bidder.MasterTableView.GetSelectedItems())
            {
                BidderInfo info = new BidderInfo();
                info.Seq = Convert.ToInt32(data.OwnerTableView.DataKeyValues[data.ItemIndex]["Seq"]);
                infoList.Add(info);
            }

            if (infoList.Count == 0)
            {
                RadManager1.Alert(SystemInfo._DeleteNull);
                return;
            }

            try
            {
                bllBidder.DeleteFromDB(infoList);
            }
            catch
            {
                RadManager1.Alert(SystemInfo._DeleteError);
                return;
            }

            LoadGridBidder();
        }

        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            BidderInfo info = new BidderInfo();
            MasterContactInfo infoContact = new MasterContactInfo();
            BidInfo infoBid = new BidInfo();
            RecordInfo infoRecord = new RecordInfo();
            
            Nullable<DateTime> receiveDate = (txtDateContactReceive.Text != "") ? (Nullable<DateTime>)Convert.ToDateTime(txtDateContactReceive.TextWithLiterals) : null;
            Nullable<TimeSpan> receiveTime = (txtTimeReceiveContact.Text.Length > 0 ? (Nullable<TimeSpan>)TimeSpan.Parse(txtTimeReceiveContact.TextWithLiterals) : null);
            Nullable<int> bidSeq = GetSeq();
            Nullable<int> contactSeq = Convert.ToInt32(cbContactReceive.SelectedValue);

            infoBid.Seq = bidSeq;
            infoContact.Seq = contactSeq;
            info.ReceiveDate = receiveDate;
            info.ReceiveTime = receiveTime;
            infoRecord.Created_by = cn.UserID;
            infoRecord.Created_when = DateTime.Now;

            info.InfoMasContact = infoContact;
            info.InfoBid = infoBid;
            info.InfoRecord = infoRecord;

            try
            {
                bllBidder.InsertFromBid(info);
                LoadGridBidder();
            }
            catch
            {
                RadManager1.Alert(SystemInfo._InsertError);
                return;
            }
        }

        #endregion

        //-----------------------------------------------------------------------------------------------

        #region tab สรุปผล

        protected void LoadJudgement()
        {
            int seq = GetSeq();
            JudgementInfo info = bllBidderQuo.GetJudgementFromBid(seq);
            lblBudgetAmount.Text = (info.BudgetAmount == null) ? "0" : ((double)info.BudgetAmount).ToString("#,##0.00");
            lblPurchaseAmount.Text = (info.PurchaseAmount == null) ? "0" : ((double)info.PurchaseAmount).ToString("#,##0.00");
            lblResult.Text = info.ResultName;
        }

        protected void LoadGridJudgement()
        {
            int seq = GetSeq();
            IList<BidderQuotationInfo> infoList = bllBidderQuo.GetListJudgementFromBid(seq);
            Grid_Judgement.DataSource = infoList;
            Grid_Judgement.MasterTableView.GroupByExpressions.Clear();
            GridGroupByExpression geItemDescr = new GridGroupByExpression(Grid_Judgement.Columns[0]);
            Grid_Judgement.MasterTableView.GroupByExpressions.Add(geItemDescr);
            Grid_Judgement.DataBind();
        }

        protected void Grid_Judgement_ItemCommand(object source, GridCommandEventArgs e)
        {
            LoadGridJudgement();
        }

        #endregion

        //-----------------------------------------------------------------------------------------------

        #region tab เอกสารแนบ

        protected void LoadGridFile()
        {
            int seq = GetSeq();
            Grid_File.DataSource = bllFile.GetDocument(seq, "ICM_BID_FILE", "ICM_BID_SEQ", "ICM_BID_FILE_SEQ");
            Grid_File.DataBind();

            cmdFac.GridCommand(panelList_File, panelGrid_File, panelCommand_File);
        }

        protected void Grid_File_ItemCommand(object source, GridCommandEventArgs e)
        {
            LoadGridFile();
        }

        protected void AddNew_File_Click(object sender, EventArgs e)
        {
            RequiredFieldValidator valFileName = new RequiredFieldValidator();
            valFileName.ID = "ValtxtFileName";
            valFileName.ControlToValidate = "txtFileName";
            valFileName.SetFocusOnError = true;
            valFileName.Display = ValidatorDisplay.Dynamic;
            valFileName.ErrorMessage = "* กรุณาระบุชื่อไฟล์ *";
            panelValtxtFileName.Controls.Add(valFileName);
            RequiredFieldValidator valAttachment = new RequiredFieldValidator();
            valAttachment.ID = "Valattachment";
            valAttachment.ControlToValidate = "attachment";
            valAttachment.SetFocusOnError = true;
            valAttachment.Display = ValidatorDisplay.Dynamic;
            valAttachment.ErrorMessage = "* กรุณาระบุที่อยู่ไฟล์ *";
            panelValattachment.Controls.Add(valAttachment);
            cmdFac.AddNewCommand(panelList_File, panelGrid_File, panelCommand_File);
        }

        protected void AddNewSave_File_Click(object sender, EventArgs e)
        {
            int seq = GetSeq();
            FileInfo infoFile = new FileInfo(hdSourceFile.Value);
            DocumentInfo info = new DocumentInfo();
            RecordInfo infoRecord = new RecordInfo();
            infoRecord.Created_by = cn.UserID;
            infoRecord.Created_when = DateTime.Now;
            info.File_info = infoFile;
            info.Name = txtFileName.Text;
            info.Dest_path = hdSourceFile.Value;
            info.InfoRecord = infoRecord;

            try
            {
                bllFile.InsertFileToDB(info, "ICM_BID_FILE", "ICM_BID_SEQ", seq);
                LoadGridFile();
            }
            catch
            {
                RadManager1.Alert(SystemInfo._InsertError);
                return;
            }
        }

        protected void Delete_File_Click(object sender, EventArgs e)
        {
            IList<DocumentInfo> infoList = new List<DocumentInfo>();
            foreach (GridDataItem data in Grid_File.MasterTableView.GetSelectedItems())
            {
                DocumentInfo info = new DocumentInfo();
                RecordInfo infoRecord = new RecordInfo();
                info.Seq = Convert.ToInt32(data.OwnerTableView.DataKeyValues[data.ItemIndex]["Seq"]);
                infoRecord.Updated_by = Int32.Parse(Session["EmpSeq"].ToString());
                infoRecord.Updated_when = DateTime.Now;
                info.InfoRecord = infoRecord;
                infoList.Add(info);
            }

            if (infoList.Count == 0)
            {
                RadManager1.Alert(SystemInfo._DeleteNull);
                return;
            }

            try
            {
                bllFile.DeleteFileFromDB(infoList, "ICM_BID_FILE", "ICM_BID_FILE_SEQ");
            }
            catch
            {
                RadManager1.Alert(SystemInfo._DeleteError);
                return;
            }

            LoadGridFile();
        }

        protected void ShowGrid_File_Click(object sender, EventArgs e)
        {
            LoadGridFile();
        }

        protected void OnAjaxUpdate_History(object sender, ToolTipUpdateEventArgs args)
        {
            this.UpdateToolTip_History(args.Value, args.UpdatePanel);
        }

        private void UpdateToolTip_History(string elementID, UpdatePanel panel)
        {
            int seq = GetSeq();
            Session["BidSeq"] = seq;
            Control ctrl = Page.LoadControl("../UserControl/TIP_Bid_File_History.ascx");
            panel.ContentTemplateContainer.Controls.Add(ctrl);
        }

        #endregion

        //-----------------------------------------------------------------------------------------------

        #region tab ข้อมูลรายงาน

        protected void LoadGrid_Report()
        {
            IList<SysReportInfo> infoList = bllReport.GetReportName("BID_APPROVE");
            Grid_Report.DataSource = infoList;
            Grid_Report.DataBind();
        }

        protected void Grid_Report_ItemCommand(object source, GridCommandEventArgs e)
        {
            LoadGrid_Report();
        }

        #endregion

        //-----------------------------------------------------------------------------------------------

        #region tab ความคืบหน้า

        ProgressPurchaseRequestBLL bllPpr = new ProgressPurchaseRequestBLL();

        protected void LoadGridApprove()
        {
            //int seq = 41;// Convert.ToInt32(Request.QueryString["Seq"]);
            //IList<PrePurchaseRequestRouteInfo> InfoPprRouteList = bllPpr.GetPrePurchaseRequestApprove(seq);
            //RadGrid TmpGrid = (RadGrid)RadPanelBar1.FindItemByValue("Bar1").FindControl("GridApprove");
            //TmpGrid.DataSource = InfoPprRouteList;
            //TmpGrid.DataBind();
        }

        protected void LoadGridInform()
        {
            //int seq = 41;// Convert.ToInt32(Request.QueryString["Seq"]);
            //IList<PrePurchaseRequestRouteInfo> InfoPprRouteList = bllPpr.GetPrePurchaseRequestInform(seq);
            //RadGrid TmpGrid2 = (RadGrid)RadPanelBar1.FindItemByValue("Bar1").FindControl("GridInform");
            //GridGroupByExpression g;
            //TmpGrid2.DataSource = InfoPprRouteList;
            //TmpGrid2.MasterTableView.GroupByExpressions.Clear();
            //g = new GridGroupByExpression(TmpGrid2.Columns[0]);
            //TmpGrid2.MasterTableView.GroupByExpressions.Add(g);

            //TmpGrid2.Columns[0].Visible = false;
            //TmpGrid2.DataBind();
        }

        protected void LoadGridRequest()
        {
            //int seq = 41;// Convert.ToInt32(Request.QueryString["Seq"]);
            //IList<PrePurchaseRequestInfo> InfoPpr = bllPpr.GetPrePurchaseRequest(seq);
            //RadGrid TmpGrid = (RadGrid)RadPanelBar1.FindItemByValue("Bar2").FindControl("GridRequest");
            //TmpGrid.DataSource = InfoPpr;
            //TmpGrid.DataBind();
        }

        protected void LoadGridPublishing()
        {
            //int seq = 41;// Convert.ToInt32(Request.QueryString["Seq"]);
            //IList<BidInfo> InfoBid = bllPpr.GetBid(seq);
            //RadGrid TmpGrid = (RadGrid)RadPanelBar1.FindItemByValue("Bar3").FindControl("GridPublishing");
            //TmpGrid.DataSource = InfoBid;
            //TmpGrid.DataBind();
        }

        protected void LoadGridCommand()
        {
            //int seq = 41;// Convert.ToInt32(Request.QueryString["Seq"]);
            //IList<PurchaseOrderInfo> InfopurOrder = bllPpr.GetPurchaseOrder(seq);
            //RadGrid TmpGrid = (RadGrid)RadPanelBar1.FindItemByValue("Bar4").FindControl("GridCommand");
            //TmpGrid.DataSource = InfopurOrder;
            //TmpGrid.DataBind();
        }

        protected void LoadGridCheck()
        {
            //int seq = 41;// Convert.ToInt32(Request.QueryString["Seq"]);
            //IList<DeliveryNoteInfo> InfoDeliveryNote = bllPpr.GetDeliveryNote(seq);
            //RadGrid TmpGrid = (RadGrid)RadPanelBar1.FindItemByValue("Bar5").FindControl("GridCheck");
            //TmpGrid.DataSource = InfoDeliveryNote;
            //TmpGrid.DataBind();
        }

        protected void GridApprove_ItemCommand(object source, GridCommandEventArgs e)
        {
            //LoadGridApprove();
        }

        protected void GridHistory_ItemCommand(object source, GridCommandEventArgs e)
        {
            //LoadGridApproveHistory();
        }

        protected void GridInformHis_ItemCommand(object source, GridCommandEventArgs e)
        {
            //LoadGridInformHistory();
        }

        protected void GridInform_ItemCommand(object source, GridCommandEventArgs e)
        {
            //LoadGridInform();
        }

        protected void GridRequest_ItemCommand(object source, GridCommandEventArgs e)
        {
            //LoadGridRequest();
        }

        protected void GridRequest_ItemDataBound(object sender, GridItemEventArgs e)
        {
            //if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            //{
            //    GridDataItem dataBoundItem = e.Item as GridDataItem;
            //    Control target = e.Item.FindControl("lkName");
            //    Control targetRefNo = e.Item.FindControl("lkRefNo");
            //    if (!Object.Equals(target, null))
            //    {
            //        if (!Object.Equals(this.TipApproveDetail, null))
            //        {
            //            this.TipApproveDetail.TargetControls.Add(target.ClientID, "Item:" + dataBoundItem["Ref_no"].Text + ":prr", true);
            //            this.TipApproveDetail.TargetControls.Add(targetRefNo.ClientID, "Item:" + dataBoundItem["Ref_no"].Text + ":prr", true);
            //        }
            //    }
            //}
        }

        protected void GridPublishing_ItemCommand(object source, GridCommandEventArgs e)
        {
            //LoadGridPublishing();
        }

        protected void GridPublishing_ItemDataBound(object sender, GridItemEventArgs e)
        {
            //if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            //{
            //    GridDataItem dataBoundItem = e.Item as GridDataItem;
            //    Control target = e.Item.FindControl("lkName");
            //    Control targetRefNo = e.Item.FindControl("lkRefNo");
            //    if (!Object.Equals(target, null))
            //    {
            //        if (!Object.Equals(this.TipApproveDetail, null))
            //        {
            //            this.TipApproveDetail.TargetControls.Add(target.ClientID, "Item:" + dataBoundItem["Ref_no"].Text + ":br", true);
            //            this.TipApproveDetail.TargetControls.Add(targetRefNo.ClientID, "Item:" + dataBoundItem["Ref_no"].Text + ":br", true);
            //        }
            //    }
            //}
        }

        protected void GridCommand_ItemCommand(object source, GridCommandEventArgs e)
        {
            //LoadGridCommand();
        }

        protected void GridCommand_ItemDataBound(object sender, GridItemEventArgs e)
        {
            //    if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            //    {
            //        GridDataItem dataBoundItem = e.Item as GridDataItem;
            //        Control target = e.Item.FindControl("lkName");
            //        Control targetRefNo = e.Item.FindControl("lkRefNo");
            //        if (!Object.Equals(target, null))
            //        {
            //            if (!Object.Equals(this.TipApproveDetail, null))
            //            {
            //                this.TipApproveDetail.TargetControls.Add(target.ClientID, "Item:" + dataBoundItem["Ref_no"].Text + ":por", true);
            //                this.TipApproveDetail.TargetControls.Add(targetRefNo.ClientID, "Item:" + dataBoundItem["Ref_no"].Text + ":por", true);
            //            }
            //        }
            //    }
            //}

            //protected void GridCheck_ItemCommand(object source, GridCommandEventArgs e)
            //{
            //    LoadGridCheck();
            //}

            //protected void GridCheck_ItemDataBound(object sender, GridItemEventArgs e)
            //{
            //    if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            //    {
            //        GridDataItem dataBoundItem = e.Item as GridDataItem;
            //        Control target = e.Item.FindControl("lkName");
            //        Control targetRefNo = e.Item.FindControl("lkRefNo");
            //        if (!Object.Equals(target, null))
            //        {
            //            if (!Object.Equals(this.TipApproveDetail, null))
            //            {
            //                this.TipApproveDetail.TargetControls.Add(target.ClientID, "Item:" + dataBoundItem["Ref_no"].Text + ":dnr", true);
            //                this.TipApproveDetail.TargetControls.Add(targetRefNo.ClientID, "Item:" + dataBoundItem["Ref_no"].Text + ":dnr", true);
            //            }
            //        }
            //    }
        }

        protected void GridCheck_ItemCommand(object source, GridCommandEventArgs e)
        {
            //LoadGridCommand();
        }

        protected void GridCheck_ItemDataBound(object sender, GridItemEventArgs e)
        {
            //    if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            //    {
            //        GridDataItem dataBoundItem = e.Item as GridDataItem;
            //        Control target = e.Item.FindControl("lkName");
            //        Control targetRefNo = e.Item.FindControl("lkRefNo");
            //        if (!Object.Equals(target, null))
            //        {
            //            if (!Object.Equals(this.TipApproveDetail, null))
            //            {
            //                this.TipApproveDetail.TargetControls.Add(target.ClientID, "Item:" + dataBoundItem["Ref_no"].Text + ":por", true);
            //                this.TipApproveDetail.TargetControls.Add(targetRefNo.ClientID, "Item:" + dataBoundItem["Ref_no"].Text + ":por", true);
            //            }
            //        }
            //    }
            //}

            //protected void GridCheck_ItemCommand(object source, GridCommandEventArgs e)
            //{
            //    LoadGridCheck();
            //}

            //protected void GridCheck_ItemDataBound(object sender, GridItemEventArgs e)
            //{
            //    if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            //    {
            //        GridDataItem dataBoundItem = e.Item as GridDataItem;
            //        Control target = e.Item.FindControl("lkName");
            //        Control targetRefNo = e.Item.FindControl("lkRefNo");
            //        if (!Object.Equals(target, null))
            //        {
            //            if (!Object.Equals(this.TipApproveDetail, null))
            //            {
            //                this.TipApproveDetail.TargetControls.Add(target.ClientID, "Item:" + dataBoundItem["Ref_no"].Text + ":dnr", true);
            //                this.TipApproveDetail.TargetControls.Add(targetRefNo.ClientID, "Item:" + dataBoundItem["Ref_no"].Text + ":dnr", true);
            //            }
            //        }
            //    }
        }

        protected void OnAjaxUpdate_AppDet(object sender, ToolTipUpdateEventArgs args)
        {
            //this.UpdateToolTip_AppDet(args.Value, args.UpdatePanel);
        }

        private void UpdateToolTip_AppDet(string elementID, UpdatePanel panel)
        {
            //string[] all_value = elementID.Split(new string[] { ":" }, StringSplitOptions.None);
            ////if (all_value[0] == "Item")
            ////{
            ////    Session["Resource_Item_Id"] = all_value[1];
            ////    Session["Expense_Id"] = null;
            ////}
            ////else
            ////{
            ////    Session["Resource_Item_Id"] = null;
            ////    Session["Expense_Id"] = all_value[1];
            ////}
            //Session["RefNo"] = all_value[1];
            //Session["DisType"] = all_value[2];
            //Control ctrl = Page.LoadControl("../UserControl/TIP_Approve_Detail.ascx");
            //panel.ContentTemplateContainer.Controls.Add(ctrl);
        }

        protected void OnAjaxUpdate_Approve_History(object sender, ToolTipUpdateEventArgs args)
        {
            //this.UpdateToolTip_Approve_History(args.Value, args.UpdatePanel);
        }

        protected void OnAjaxUpdate_Inform_History(object sender, ToolTipUpdateEventArgs args)
        {
            //this.UpdateToolTip_Inform_History(args.Value, args.UpdatePanel);
        }

        private void UpdateToolTip_Approve_History(string elementID, UpdatePanel panel)
        {
            //Session["HisSeq"] = elementID;
            //Control ctrl;
            //ctrl = Page.LoadControl("../UserControl/ICM_PPR_RouteHistoryApprove.ascx");
            //panel.ContentTemplateContainer.Controls.Add(ctrl);
        }

        private void UpdateToolTip_Inform_History(string elementID, UpdatePanel panel)
        {
            //Session["HisSeq"] = elementID;
            //Control ctrl;
            //ctrl = Page.LoadControl("../UserControl/ICM_PPR_RouteHistoryInform.ascx");
            //panel.ContentTemplateContainer.Controls.Add(ctrl);
        }

        protected void btnHistory_Click(object sender, EventArgs e)
        {

        }

        protected void btnHistory2_Click(object sender, EventArgs e)
        {

        }

        #endregion

        #region คัดเลือก

        protected void LoadGridSelect()
        {
            Grid_Select.Columns.Clear();
            Grid_Hide_Select.Columns.Clear();
            DataTable dtb = new DataTable();
            DataTable dtbHide = new DataTable();
            int seq = GetSeq();
            IList<BidderInfo> infoBidder = bllBidder.GetBidderFromBid(seq);
            if (infoBidder.Count > 0)
            {
                dtb.Columns.Add("ITEM_DESCR");
                dtbHide.Columns.Add("ITEM_DESCR");
                GridTemplateColumn tcc = new GridTemplateColumn();
                tcc.ItemTemplate = new ItemTemplate.CreateItemTemplateLabel("ITEM_DESCR", "lblItemDescr", GridWidth + 50);
                tcc.HeaderText = "รายการพัสดุ";
                tcc.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
                tcc.HeaderStyle.VerticalAlign = VerticalAlign.Top;
                tcc.ItemStyle.VerticalAlign = VerticalAlign.Top;
                Grid_Select.Columns.Add(tcc);
                Grid_Hide_Select.Columns.Add(tcc);
                for (int i = 0; i < infoBidder.Count; i++)
                {
                    GridTemplateColumn tc = new GridTemplateColumn();
                    GridTemplateColumn tcHide = new GridTemplateColumn();
                    if (hdSelectStatus.Value.Length > 0)//(isSelectInEditMode)
                    {
                        tc.ItemTemplate = new ItemTemplate.CreateItemTemplateCheckBox(infoBidder[i].InfoMasContact.Name_th, "chk" + infoBidder[i].Seq.ToString(), GridWidth);
                    }
                    else
                    {
                        tc.ItemTemplate = new ItemTemplate.CreateItemTemplateLabel(infoBidder[i].InfoMasContact.Name_th, "lbl" + infoBidder[i].Seq.ToString(), GridWidth);
                    }
                    tcHide.ItemTemplate = new ItemTemplate.CreateItemTemplateLabel(infoBidder[i].InfoMasContact.Name_th, "lbl" + infoBidder[i].Seq.ToString(), GridWidth);
                    tc.HeaderText = (infoBidder[i].InfoVat.Seq == 3
                            ? infoBidder[i].InfoMasContact.Name_th + "</br>" + infoBidder[i].InfoVat.Name
                            : infoBidder[i].InfoMasContact.Name_th + "</br>ราคารวมภาษีมูลค่าเพิ่ม " + infoBidder[i].VatPercent.ToString() + "%");
                    tcHide.HeaderText = (infoBidder[i].InfoVat.Seq == 3
                            ? infoBidder[i].InfoMasContact.Name_th + "</br>" + infoBidder[i].InfoVat.Name
                            : infoBidder[i].InfoMasContact.Name_th + "</br>ราคารวมภาษีมูลค่าเพิ่ม " + infoBidder[i].VatPercent.ToString() + "%");
                    tc.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
                    tc.HeaderStyle.VerticalAlign = VerticalAlign.Top;
                    tc.ItemStyle.VerticalAlign = VerticalAlign.Top;
                    Grid_Select.Columns.Add(tc);
                    Grid_Hide_Select.Columns.Add(tcHide);
                    dtb.Columns.Add(infoBidder[i].InfoMasContact.Name_th);
                    dtbHide.Columns.Add(infoBidder[i].InfoMasContact.Name_th);
                }

                IList<BidderQuotationInfo> infoItem = bllBidderQuo.GetItemNameFromBid(seq);
                if (infoItem.Count > 0)
                {
                    for (int i = 0; i < infoItem.Count; i++)
                    {
                        IList<BidderQuotationInfo> infoQuotation = bllBidderQuo.GetBidderQuotationFromPurchaseRequestItemWithBid(seq, Convert.ToInt32(infoItem[i].InfoPrItem.Seq));
                        if (infoQuotation.Count > 0)
                        {
                            while (infoQuotation.Count > 0)
                            {
                                DataRow dr = dtb.NewRow();
                                DataRow drHide = dtbHide.NewRow();
                                for (int j = 1; j < Grid_Select.Columns.Count; j++)
                                {
                                    dr["ITEM_DESCR"] = infoItem[i].InfoPrItem.Descr;
                                    drHide["ITEM_DESCR"] = infoItem[i].InfoPrItem.Descr;
                                    for (int k = 0; k < infoQuotation.Count; k++)
                                    {
                                        string contact_name = (infoQuotation[k].InfoBidder.InfoVat.Seq == 3
                                            ? infoQuotation[k].InfoBidder.InfoMasContact.Name_th + "</br>" + infoQuotation[k].InfoBidder.InfoVat.Name
                                            : infoQuotation[k].InfoBidder.InfoMasContact.Name_th + "</br>ราคารวมภาษีมูลค่าเพิ่ม " + infoQuotation[k].InfoBidder.VatPercent.ToString() + "%");
                                        if (contact_name == Grid_Select.Columns[j].HeaderText)
                                        {
                                            if (dr[infoQuotation[k].InfoBidder.InfoMasContact.Name_th].ToString().Length > 0)
                                            {
                                                continue;
                                            }
                                            else
                                            {
                                                dr[infoQuotation[k].InfoBidder.InfoMasContact.Name_th] = infoQuotation[k].Descr;
                                                drHide[infoQuotation[k].InfoBidder.InfoMasContact.Name_th] = infoQuotation[k].Remarks;
                                                infoQuotation.Remove(infoQuotation[k]);
                                            }
                                        }
                                    }
                                }
                                dtb.Rows.Add(dr);
                                dtbHide.Rows.Add(drHide);
                            }
                        }
                        else
                        {
                            DataRow dr = dtb.NewRow();
                            DataRow drHide = dtbHide.NewRow();
                            dr["ITEM_DESCR"] = infoItem[i].InfoPrItem.Descr;
                            drHide["ITEM_DESCR"] = infoItem[i].InfoPrItem.Descr;
                            dtb.Rows.Add(dr);
                            dtbHide.Rows.Add(drHide);
                        }
                    }
                }
            }

            Grid_Select.Width = (Grid_Select.Columns.Count + 1) * GridWidth;
            Grid_Select.DataSource = dtb;
            Grid_Select.DataBind();

            Grid_Hide_Select.DataSource = dtbHide;
            Grid_Hide_Select.DataBind();

            string strItemDescr = "";
            for (int i = 0; i < Grid_Select.Items.Count; i++)
            {
                if (i > 0)
                {
                    if (strItemDescr == ((Label)Grid_Select.Items[i].FindControl("lblItemDescr")).Text)
                    {
                        strItemDescr = ((Label)Grid_Select.Items[i].FindControl("lblItemDescr")).Text;
                        ((Label)Grid_Select.Items[i].FindControl("lblItemDescr")).Text = "";
                    }
                    else
                        strItemDescr = ((Label)Grid_Select.Items[i].FindControl("lblItemDescr")).Text;
                }
                else
                {
                    strItemDescr = ((Label)Grid_Select.Items[i].FindControl("lblItemDescr")).Text;
                }
            }

            if (hdSelectStatus.Value.Length > 0)//(isSelectInEditMode)
            {
                for (int i = 0; i < Grid_Hide_Select.Items.Count; i++)
                {
                    for (int j = 0; j < infoBidder.Count; j++)
                    {
                        if (((Label)Grid_Hide_Select.Items[i].FindControl("lbl" + infoBidder[j].Seq.ToString())).Text.Length > 0)
                        {
                            string[] astrKeyID = ((Label)Grid_Hide_Select.Items[i].FindControl("lbl" + infoBidder[j].Seq.ToString())).Text.Split(';');
                            if (astrKeyID[1] == SystemInfo._YesStatus)
                            {
                                ((CheckBox)Grid_Select.Items[i].FindControl("chk" + infoBidder[j].Seq.ToString())).Checked = true;
                            }
                            if (astrKeyID[2] == astrKeyID[3])
                            {
                                ((CheckBox)Grid_Select.Items[i].FindControl("chk" + infoBidder[j].Seq.ToString())).ForeColor = System.Drawing.Color.Red;
                            }
                        }
                    }
                }
            }
            else
            {
                for (int i = 0; i < Grid_Hide_Select.Items.Count; i++)
                {
                    for (int j = 0; j < infoBidder.Count; j++)
                    {
                        if (((Label)Grid_Hide_Select.Items[i].FindControl("lbl" + infoBidder[j].Seq.ToString())).Text.Length > 0)
                        {
                            string[] astrKeyID = ((Label)Grid_Hide_Select.Items[i].FindControl("lbl" + infoBidder[j].Seq.ToString())).Text.Split(';');
                            if (astrKeyID[1] == SystemInfo._YesStatus)
                            {
                                ((Label)Grid_Select.Items[i].FindControl("lbl" + infoBidder[j].Seq.ToString())).BackColor = System.Drawing.Color.LightSteelBlue;
                            }
                            if (astrKeyID[2] == astrKeyID[3])
                            {
                                ((Label)Grid_Select.Items[i].FindControl("lbl" + infoBidder[j].Seq.ToString())).ForeColor = System.Drawing.Color.Red;
                            }
                        }
                    }
                }
            }
        }

        protected void Edit_Select_Click(object sender, EventArgs e)
        {
            Edit_Select.Visible = false;
            EditSave_Select.Visible = true;
            Cancel_Select.Visible = true;

            hdSelectStatus.Value = "1";
            LoadGridSelect();
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            int seq = GetSeq();
            IList<BidderQuotationInfo> infoList = new List<BidderQuotationInfo>();
            IList<BidderInfo> infoBidder = bllBidder.GetBidderFromBid(seq);
            CheckBox chk;

            if (infoBidder.Count > 0)
            {
                for (int i = 0; i < Grid_Hide_Select.Items.Count; i++)
                {
                    for (int j = 0; j < infoBidder.Count; j++)
                    {
                        if (((Label)Grid_Hide_Select.Items[i].FindControl("lbl" + infoBidder[j].Seq.ToString())).Text.Length > 0)
                        {
                            chk = (CheckBox)Grid_Select.Items[i].FindControl("chk" + infoBidder[j].Seq.ToString());
                            if (chk != null)
                            {
                                if (chk.Checked)
                                {
                                    BidderQuotationInfo info = new BidderQuotationInfo();
                                    RecordInfo infoRecord = new RecordInfo();
                                    string[] astrKeyID = ((Label)Grid_Hide_Select.Items[i].FindControl("lbl" + infoBidder[j].Seq.ToString())).Text.Split(';');
                                    string quo_selected = astrKeyID[1];

                                    if (quo_selected != SystemInfo._YesStatus)
                                    {
                                        info.Seq = Convert.ToInt32(astrKeyID[0]);
                                        info.Selected = SystemInfo._YesStatus;
                                        info.SelectedReason = txtSelectReason.Text;
                                        infoRecord.Updated_by = 1;
                                        infoRecord.Updated_when = DateTime.Now;
                                        info.InfoRecord = infoRecord;
                                        infoList.Add(info);
                                    }
                                }
                                else
                                {
                                    BidderQuotationInfo info = new BidderQuotationInfo();
                                    RecordInfo infoRecord = new RecordInfo();
                                    string[] astrKeyID = ((Label)Grid_Hide_Select.Items[i].FindControl("lbl" + infoBidder[j].Seq.ToString())).Text.Split(';');
                                    string quo_selected = astrKeyID[1];

                                    if (quo_selected != SystemInfo._NoStatus)
                                    {
                                        info.Seq = Convert.ToInt32(astrKeyID[0]);
                                        info.Selected = SystemInfo._NoStatus;
                                        info.SelectedReason = txtSelectReason.Text;
                                        infoRecord.Updated_by = 1;
                                        infoRecord.Updated_when = DateTime.Now;
                                        info.InfoRecord = infoRecord;
                                        infoList.Add(info);
                                    }
                                }
                            }
                        }
                    }
                }
                if (infoList.Count > 0)
                {
                    bllBidderQuo.SelectedBidderQuotation(infoList);
                }
                Cancel_Select_Click(null, null);
            }
        }

        protected void Cancel_Select_Click(object sender, EventArgs e)
        {
            Edit_Select.Visible = true;
            EditSave_Select.Visible = false;
            Cancel_Select.Visible = false;

            hdSelectStatus.Value = "";
            LoadGridSelect();
        }

        #endregion
    }
}
